//
//  BrandCar.m
//  AutoHome
//
//  Created by wqh on 15-5-11.
//  Copyright (c) 2015年 Wqh. All rights reserved.
//

#import "BrandCar.h"

#import "BrandCarModel.h"
#import "BrandCarCell.h"


#define kBrandCarCellId @"brandCarCellId"
@interface BrandCar ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic) NSMutableArray *allBrandArr;
@property (nonatomic) NSMutableArray *letterArr;
@property (nonatomic) UITableView *tableView;

@property (nonatomic) UILabel *indexLabel;
@end

@implementation BrandCar

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor randomColor];
   
    [self createTableView ];
    [self createIndexView];
    [self initData];
    [self createBrandCarView];
}

- (void)initData {
    
    self.allBrandArr = [NSMutableArray array];
    self.letterArr = [NSMutableArray array];
    
    NSArray *array = [[DataEngine sharedInstance] parseBrandData];
    self.allBrandArr = array[1];
    self.letterArr = array[0];
    
    
    [self.letterArr insertObjects:@[@"★",@"热销车"] atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 2)]];
    
    
    BrandCarModel *myModel = [[BrandCarModel alloc] init];
    BrandCarModel *collectModel = [[BrandCarModel alloc] init];
    myModel.name = @"我的收藏";
    myModel.imgurl = [[NSBundle mainBundle] pathForResource:@"findcar_icon_collect@2x" ofType:@"png"];
    collectModel.name = @"热销车";
    collectModel.imgurl = [[NSBundle mainBundle] pathForResource:@"icon_hot@2x" ofType:@"png"];
    
    [self.allBrandArr insertObjects:@[@[myModel],@[collectModel]] atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 2)]];

    [self.tableView reloadData];
    
}

- (void)createBrandCarView {
    self.brandCarView = [[[NSBundle mainBundle] loadNibNamed:@"BrandCarView" owner:self options:nil] lastObject];
    [self.view addSubview:self.brandCarView];
    self.brandCarView.frame = CGRectMake(kScreenWidth, 0, kScreenWidth-70, kScreenHeight-64-49);
    __weak typeof(self) mySelf = self;
//    self.brandCarView.myPicJumpBlock = ^ (UIViewController *vc) {
//        [mySelf presentViewController:vc animated:YES completion:nil];
//    };
    self.brandCarView.hidden = YES;
}

- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-64-49) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"BrandCarCell" bundle:nil] forCellReuseIdentifier:kBrandCarCellId];
    
    [self.view addSubview:self.tableView];
}

#pragma mark - 创建索引显示图片
- (void)createIndexView {
    self.indexLabel = [[UILabel alloc] init];
    self.indexLabel.bounds = CGRectMake(0, 0, 100, 100);
    self.indexLabel.center = self.tableView.center;
    self.indexLabel.text = @"A";
    self.indexLabel.font = [UIFont boldSystemFontOfSize:45];
    self.indexLabel.alpha = 0;
    [self.indexLabel setTextColor:[UIColor customBlueColor]];
    [self.view addSubview:self.indexLabel];
}

- (void)displayIndexView:(NSString *)title {
    self.indexLabel.text = title;
    self.indexLabel.alpha = 1;
    [UIView animateWithDuration:0.5 animations:^{
        self.indexLabel.alpha = 0;
    } completion:^(BOOL finished) {
        
    }];
}

#pragma mark UITableViewDataSource {

//增加右侧索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    NSMutableArray *indexArr = [NSMutableArray arrayWithArray:self.letterArr];
    [indexArr removeObjectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 2)]];
    return indexArr;
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    [self displayIndexView:self.letterArr[index+2]];
    return index+2;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.letterArr.count ;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.allBrandArr[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BrandCarCell  *cell = [tableView dequeueReusableCellWithIdentifier:kBrandCarCellId forIndexPath:indexPath];
    
    BrandCarModel *model = self.allBrandArr[indexPath.section][indexPath.row];
    [cell showDataWithModel:model];
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return self.letterArr[section];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 22;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    BrandCarModel *brandModel = self.allBrandArr[indexPath.section][indexPath.row];
    
    self.brandCarView.brandModel = brandModel;
    self.brandCarView.segmentedControl.selectedSegmentIndex = 0;
    self.brandCarView.hidden = NO;
    [UIView animateWithDuration:0.5 animations:^{
        
        self.brandCarView.frame = CGRectMake(70, 0, kScreenWidth-70, kScreenHeight-64-49);
    } completion:^(BOOL finished) {
     
    }];
    
    //点击cell的时候通知brandView下载数据
    [[NSNotificationCenter defaultCenter] postNotificationName:@"BrandCarDisplay" object:nil];
    
}

#pragma mark - UIScrollViewDelegate 
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    
    if (scrollView == self.tableView) {
        [UIView animateWithDuration:0.5 animations:^{
            self.brandCarView.frame = CGRectMake(kScreenWidth, 0, kScreenWidth-70, kScreenHeight-64-49);
        } completion:^(BOOL finished) {
            
        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
